package org.itboys.job;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.itboys.admin.entity.AdminUser;
import org.itboys.admin.entity.GlobalConfigDB;
import org.itboys.admin.service.AdminUserService;
import org.itboys.admin.service.GlobalConfigService;
import org.itboys.seller.constant.GlobalConfigConstants;
import org.itboys.seller.entity.*;
import org.itboys.seller.meta.Account;
import org.itboys.seller.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hzliangchong1@corp.netease.com
 *         Date 2016/10/27.
 */
@Service
public class MatchJob {

    private static final Logger logger = LoggerFactory.getLogger(MatchJob.class);

    @Resource
    private AccountService accountService;
    @Resource
    private MatchInfoService matchInfoService;
    @Resource
    private CaliforniaSiteUserService californiaSiteUserService;
    @Resource
    private SellerUserService sellerUserService;
    @Resource
    private GlobalConfigService globalConfigService;
    @Resource
    private AdminUserService adminUserService;
    @Resource
    private ChangeScoreRecordService changeScoreRecordService;
    @Resource
    private RankingUserService rankingUserService;
    @Resource
    private SubManProviderService subManProviderService;

    //匹配专用job
    public void startMatchJob() {
        logger.info("start match job!");
        List<GlobalConfigDB> globalConfigDBs = globalConfigService.getAllConfigs();
        Map<String, GlobalConfigDB> globalConfigDBMap = globalConfigDBs.stream().collect(Collectors.toMap(p -> p.getKey(), p -> p));
        AdminUser adminUser = adminUserService.getAdminUser("admin");
        californiaSiteUserService.startMatchJob(adminUser, globalConfigDBMap, true);
        logger.info("end match job!");
        /*logger.info("start match job!");
        List<ChangeScoreRecord> changeAll = changeScoreRecordService.getAll();
        for (ChangeScoreRecord record : changeAll) {
            record.setScoreDelta(record.getScoreDelta() * 10000);
            changeScoreRecordService.update(record);
        }
        RankingUserDB rankingUserDB = rankingUserService.getLatestRankingUserBySellerUserName(0, "");
        Map<Long, Integer> actualScores = rankingUserDB.getActualScores();
        for (Long key : actualScores.keySet()) {
            actualScores.put(key, actualScores.get(key) * 10000);
        }
        rankingUserDB.setActualScores(actualScores);
        Map<Long, Integer> totalScores = rankingUserDB.getTotalScores();
        for (Long key : totalScores.keySet()) {
            totalScores.put(key, totalScores.get(key) * 10000);
        }
        rankingUserDB.setTotalScores(totalScores);
        Map<Long, Integer> sActualScores = rankingUserDB.getsActualScores();
        for (Long key : sActualScores.keySet()) {
            sActualScores.put(key, sActualScores.get(key) * 10000);
        }
        rankingUserDB.setsActualScores(sActualScores);
        Map<Long, Integer> sTotalScores = rankingUserDB.getsTotalScores();
        for (Long key : sTotalScores.keySet()) {
            sTotalScores.put(key, sTotalScores.get(key) * 10000);
        }
        rankingUserDB.setsTotalScores(sTotalScores);
        Map<Long, Integer> ssActualScores = rankingUserDB.getSsActualScores();
        for (Long key : ssActualScores.keySet()) {
            ssActualScores.put(key, ssActualScores.get(key) * 10000);
        }
        rankingUserDB.setSsActualScores(ssActualScores);
        Map<Long, Integer> ssTotalScores = rankingUserDB.getSsTotalScores();
        for (Long key : ssTotalScores.keySet()) {
            ssTotalScores.put(key, ssTotalScores.get(key) * 10000);
        }
        rankingUserDB.setSsTotalScores(ssTotalScores);
        rankingUserDB.setTotalAmount(rankingUserDB.getTotalAmount() * 10000);

        List<SubManProviderDB> providerDBs = subManProviderService.getAll();
        for (SubManProviderDB providerDB : providerDBs) {
            providerDB.setScore(providerDB.getScore() * 10000);
            subManProviderService.update(providerDB);
        }*/
    }
}
